// =======================================================================
// 
//       Filename:  main.cpp
// 
//    Description:  
// 
//        Version:  1.0
//        Created:  27.03.2010 17:15:48
//       Revision:  none
//       Compiler:  g++
// 
//         Author:  Dominik 'domachine' Burgdörfer (-), dominik.burgdoerfer@googlemail.com
//        Company:  -
// 
// =======================================================================

#include "webplodder/plodder.hpp"
#include "webplodder/utils/pathlist.hpp"
#include "webplodder/exceptions/syntaxerror.hpp"
#include <fstream>
#include <cstdlib>
#include <boost/program_options.hpp>
#include <boost/foreach.hpp>

#define foreach BOOST_FOREACH

namespace po = boost::program_options;
using namespace webplodder;
using namespace std;
using namespace exceptions;

int main(int ac, char** av) {
    string str;
    map<string,string>
        config;

    string configPath(getenv("HOME"));
    configPath.append("/.webplodder");

    Plodder plodder(configPath);

    vector<string> inputDirectories;
    utils::PathList excludeList;

    try {
        po::options_description desc("Options");
        po::positional_options_description pos;

        desc.add_options()
            ("help,h", "produce help message")
            ("input-dir", po::value<vector<string> >(),
             "add input directory")
            ("exclude,e", po::value<vector<string> >(),
             "exclude files. Relative paths are interpreted to be"
             " relative to the project directory.")
            ;

        pos.add("input-dir", -1);

        po::command_line_parser parser(ac, av);
        parser = parser.options(desc).positional(pos);

        po::variables_map vm;
        po::store(parser.run(), vm);

        po::notify(vm);

        if(vm.count("help")) {
            cout << desc << endl;
            return 0;
        }

        if(vm.count("input-dir")) {
            inputDirectories = 
                vm["input-dir"].as<vector<string> >();
        }
        else {
            cerr << "error: no input directories" << endl;
            return 1;
        }

        if(vm.count("exclude")) {
            vector<string> exclude =
                        vm["exclude"].as<vector<string> >();

            foreach(string str, exclude)
            {
                excludeList.push_back(str);
            }
        }
    }
    catch(exception& e) {
        cerr << "error: " << e.what() << "\n";
        return 1;
    }

    try {
        vector<string>::iterator iter = 
            inputDirectories.begin();
        plodder.init();

        for(; iter != inputDirectories.end(); ++iter) {
            plodder.scanDir(*iter, excludeList);
        }
    }
    catch(SyntaxError& e) {
        cerr << e.file() << ":" << e.line()
            << ": error: " << e.what() << endl;
    }
    catch(ParsingError& e) {
        cerr << e.file() << ": " << e.what() << endl;
    }
    catch(Exception& e) {
        cerr << "webplodder: " << e.what() << endl;
    }

    return 0;
}
